﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AbstractFramework.Display;
using AbstractFramework.LogicControl;
using AbstractFramework.Constant;

namespace ControlLib
{
    /// <summary>
    /// Interaction logic for FloorButtonPanel.xaml
    /// </summary>
    public partial class OutsideButtonPanelDisplay : UserControl, IOutsideButtonPanelDisplay
    {
        private IButtonFactory buttonFactory;
        private int floorId;
        private bool isTopFloor;
        private bool isBottomFloor;

        public OutsideButtonPanelDisplay()
        {
            InitializeComponent();
        }

        public void light(Direction direction)
        {
            if (direction == Direction.up)
            {
                upBtn.Background = new SolidColorBrush(Color.FromRgb(0xDF, 0xF3, 0xA9));
            }
            else if (direction == Direction.down)
            {
                downBtn.Background = new SolidColorBrush(Color.FromRgb(0xDF, 0xF3, 0xA9));
            }
        }

        //btnID==0：表示熄灭向上的灯，btnID==1：表示熄灭向下的灯
        public void extinguish(int btnID)
        {
            if (btnID == 0)
            {
                upBtn.Background = new SolidColorBrush(Color.FromRgb(0x73, 0x7D, 0x76));
            }
            else if (btnID == 1)
            {
                downBtn.Background = new SolidColorBrush(Color.FromRgb(0x73, 0x7D, 0x76));
            }
        }

        public void initialize(IButtonFactory buttonFactory, int floorId, bool isTopFloor, bool isBottomFloor)        
        {
            if(isTopFloor)
            {
                this.upBtn.IsEnabled = false;
            }
            if(isBottomFloor)
            {
                this.downBtn.IsEnabled = false;
            }
            this.buttonFactory = buttonFactory;
            this.floorId = floorId;
            this.isTopFloor = isTopFloor;
            this.isBottomFloor = isBottomFloor;
            this.upBtn.Click += new RoutedEventHandler(upBtn_Click);
            this.downBtn.Click += new RoutedEventHandler(downBtn_Click);
        }

        public void setEnabled(bool isEnabled)
        {
            if (isEnabled)
            {
                if (isTopFloor)
                {
                    this.upBtn.IsEnabled = false;
                }
                else
                {
                    this.upBtn.IsEnabled = true;
                }
                if (isBottomFloor)
                {
                    this.downBtn.IsEnabled = false;
                }
                else
                {
                    this.downBtn.IsEnabled = true;
                }
            }
            else
            {
                this.upBtn.IsEnabled = false;
                this.downBtn.IsEnabled = false;
            }
        }

        void upBtn_Click(object sender, RoutedEventArgs e)
        {
            IOutsideButton button = (IOutsideButton)this.buttonFactory.createButton(ButtonType.outsideButton);
            button.MoveDirection = (int)Direction.up;
            button.WaitFloor = this.floorId;
            button.createButtonRequisition();
            this.light(Direction.up);//把自己点亮
        }
        
        void downBtn_Click(object sender, RoutedEventArgs e)
        {
            IOutsideButton button = (IOutsideButton)this.buttonFactory.createButton(ButtonType.outsideButton);
            button.MoveDirection = (int)Direction.down;
            button.WaitFloor = this.floorId;
            button.createButtonRequisition();
            this.light(Direction.down);//把自己点亮
        }
    }
}
